import pygame, constants

from pygame.locals import *
from constants import *

#tile class: a single tile on the map

class Tile(pygame.sprite.Sprite):
    """A single tile in the map"""
    def __init__(self, image, solid, x, y, isTransitionTile=False, transitionMap=None, action=None):
        """Creates a tile with the specified parameters"""
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        self.solid = solid
        self.isTransitionTile = isTransitionTile
        self.transitionMap = transitionMap

        if action is not None:
            self.action = action
        else:
            self.action = Action()
        
    #placeholder method in case I want to render tiles from within the tile class
    def draw(self, surface):
        """Placeholder method"""
        pass

    def getTileCoords(self):
        """Returns the coordinates of the tile in the map as a list [x, y]"""
        return [self.rect.topleft[0] / TILESIZE, self.rect.topleft[1] / TILESIZE]

#a tile's action is performed on a player when the player presses enter and is standing on that tile
class Action():
    """Represents an action that can be performed by the player on a specific tile"""
    def __init__(self):
        pass

    #returns: whether the action was successfully undertaken or not
    def doAction(self, playerMob, playerMap):
        """The actual code of the action. Returns whether the action was successful"""
        return False
    
    #returns: the list of actions that this tile supports
    def getActionList(self):
        """Returns a list of strings representing the available actions on this tile"""
        return ["No actions available"]

class GoOutsideAction(Action):
    """Causes the map the tile is in to transition to an outside state. If it is already in that state, nothing happens"""
    def __init__(self):
        Action.__init__(self)

    def doAction(self, playerMob, playerMap):
        print "Going outside"
        playerMap.generateOutsideMap()
        playerMap.scene.renderScene()
        playerMap.scene.drawStatusLabels()
        playerMap.scene.refreshGUI()
        pygame.display.update()

    def getActionList(self):
        return ["Go outside"]

class GoInsideAction(Action):
    """Causes the map the tile is in to transition to an inside state. If it is already in that state, nothing happens"""
    def __init__(self):
        Action.__init__(self)

    def doAction(self, playerMob, playerMap):
        print "Going inside"
        playerMap.generateInsideMap()
        playerMap.scene.renderScene()
        playerMap.scene.refreshGUI()
        playerMap.scene.drawStatusLabels()
        pygame.display.update()

    def getActionList(self):
        return ["Go inside"]

class SleepAction(Action):
    """Restores the player's energy and lowers their score"""
    def __init__(self):
        Action.__init__(self)

    def doAction(self, playerMob, playerMap):
        playerMob.energy = 10
        print "Player slept"
        playerMap.status = "You sleep in the bed, restoring your energy but decreasing your points."
        playerMap.scene.drawStatusLabels()
        if playerMob.points >= 5:
            playerMob.points -= 5
        pygame.display.update()
        return True

    def getActionList(self):
        return ["Sleep (restores energy)"]

